//============================================================================
// Name        : T4.cpp
// Author      : 伪尘子
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <sstream>
#include <vector>
#include <bitset>
#include <exception>
#include "Student.h"
using namespace std;
void StringIO_Test() {

	/***
	 *
	 *istringstream是输入对象，拥有>>操作符，可以将流输入到指定对象
	 *ostringstring是输出对象，拥有<<操作符，可以将流输出到自己
	 */
	int a, b, c;
	a = 10;
	b = 10;
	c = 10;
	istringstream istream("9");
	istream >> a;
	ostringstream ostream;
	ostream << 3;
	ostream << "\tA=" << a << "\t B=" << b << "\t C=" << c;
	string abc = ostream.str();
	cout << "ABC=" << abc << endl;
}
void While_Test() {

	int a = 0, b = 0;
	//C++ 中可能不存在对象空引用的问题

	cin >> a >> b;

	while (7) {

		if (a > 0)
			break;
		else {

			cout << "A=" << a << "\n" << endl;
			cout << "请输入B:" << endl;
			cin >> b;
			cout << "B=" << b << "\n" << endl;

		}
	}
}

/**
 * 修改string
 */
void Change_test(string str, int i) {

	stringstream sb;
	sb << str << i;
	str = sb.str();
	cout << str << endl;
}

void Extern_test() {
	int num = 1024;
	int &refI = num;
	string str("ok");
	const int count = 10;
	for (int i = 0; i < count; i++) {
		num /= (i + 1);

		cout << "\ti= " << i << endl;
		cout << "Num is " << num << endl;
		cout << "Ref i is " << refI << " ;" << endl;
		cout << "Str is " << str << endl;
	}

	typedef short sint;
	sint a, b, c;
	a = 10;
	b = 20;
	c = 222;
}

enum card {
	ht = 10, ht2, hx, mh, fk
};
/**
 * 枚举
 */
void Enum_test() {

	card htz = ht2;
	cout << htz << endl;
}

/**
 * 禁止关闭
 */
void DontClose() {
	int a;
	cin >> a;
}

void Class_test() {
	Student stu;
	stu.name = "Zhang Fan";
	stu.age = 20;
	stu.sex = true;
	stu.showInfo();
}

/**
 * 遍历string
 */
void ForEachString_test(string text) {

	//size_type是遍历string所必须的，它足够长
	for (string::size_type index = 0; index < text.size(); index++) {

		cout << text[index] << endl;
	}
}

/**
 * 判断string为空
 */
void Empty_test(string s) {

	bool isempty = s.empty();

	cout << "aas " << (isempty ? "男" : "女") << endl;
////	isempty=s==NULL;
//	cout<<"字符串是否等于 "<<"NULL:"<<isempty<<endl;
//	isempty=s=="";
//	cout<<"字符串是否等于 "<<" :"<<isempty<<endl;

}

void Vector_test() {
	vector<int> numlist;
	//添加数据
	for (int i = 0; i < 10; i++) {
		numlist.push_back(i);
	}
	//遍历Vector
	for (vector<int>::size_type j = 0; j < numlist.size(); j++) {
		cout << numlist[j] << endl;
	}
	int num = 10;

	//通过迭代器遍历容器
	for (vector<int>::iterator iter = numlist.begin(); iter != numlist.end();
			iter++) {

		*iter = num;
		num--;
	}
	//遍历Vector
	for (vector<int>::size_type j = 0; j < numlist.size(); j++) {
		cout << numlist[j] << endl;
	}

}

/**
 * bitset,可以直接存储二进制数据
 */
void BitSet_test() {
	cout << "size" << endl;
	bitset<32> bs(8); //直接将16进制的数据放到bs中
	string str;
	string name("张帆");
	Student stu(name, 20, true);
	stu.showInfo();
	int size = bs.size();
	cout << "size" << size << " " << bs << " string: " << " ulong: "
			<< bs.to_ulong() << endl;
	for (int i = 0; i != bs.size(); i++) {

		cout << bs[i] << endl;
	}
	cout << endl;
}

/**
 * 数组
 */
void Array_test() {

	int numlist[] = { 2, 3, 32, 2323, 23 };
	char charlist[5] = "abcd"; //直接定义的字符数组，有第5个元素
	char charlist2[4] = { 'a', 'b', 'c', 'd' }; //这里
	for (int i = 0; i < 5; i++) {
		cout << "- " << charlist2[i] << " --" << endl;
	}
	for (int i = 0; i < 5; i++) {
		cout << "2- " << charlist[i] << " --" << endl;
	}
}

/**
 * 解指针可以给原对象赋值
 */
void testReference(int *abc) {
	*abc = 1000;
}

size_t length(int array[]) {
	size_t length = 0;
//	int *P_array = array;
	cout<<array[0]<<array[1]<<endl;
	length=sizeof(&array)/sizeof(array[0]);
	cout<<length<<endl;
//	while (P_array != NULL) {
//
//		P_array++;
//		try {
//			int a = *P_array;
//
//		} catch ((const runtime_error &re) {
//			cerr << e.what() << " Try again.\n" << endl;
//			break;
//		}
//
//		length++;
//	}
//	cout << length << endl;
	return length;
}

/**
 * 指针
 */
void Point_test() {
	int abc = 0;
	int *p_abc;
	p_abc++;

	int &R_abc = abc;
	cout << "abc: " << R_abc << endl;
	R_abc = 10;
	cout << "abc: " << R_abc << endl;
	p_abc = &R_abc;
	testReference(p_abc);
	cout << "abc: " << R_abc << endl;
	cout << "asdfasdf" << endl;

	int numlist[] = { 2, 32, 23, 23, 23, 23, 23232, 10101 };
	int *P_numlist = numlist;
	size_t length_num = length(numlist);
	cout<<length_num<<" ";
//	for(size_t i=0;i<length_num;i++){
//		cout<<i<<" ";
//		cout<<P_numlist[i]<<endl;
//	}
	cout<<"End:"<<*(P_numlist+8)<<endl;
	int end=*(P_numlist+7);
	for(;;++P_numlist){
		cout<<*P_numlist<<endl;
		if(*P_numlist==end)
			break;
	}

}

int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	//StringIO_Test();
//	While_Test();
//	Extern_test();
//	Enum_test();
	string str("sa"); //("asfdasd");
//	Class_test();

//	ForEachString_test(str);
//	Empty_test(str);
//	Vector_test();
//	Vector_test();
//	BitSet_test();
//	Array_test();
	Point_test();
//	DontClose();
	return 0;
}

